package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.awt.*;
import java.util.Scanner;

public class TeacherController {
    private TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);
    public void start() {
        teacherclose : while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            
            String choice = sc.next();
            switch (choice){
                case "1":
                   // System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                   // System.out.println("删除老师");
                    deleteTeacherById();
                    break;
                case "3":
                    // System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("退出");
                    break teacherclose;
                default:
                    System.out.println("您的输入有误,请重新输入");
                    break;
            }
        }
    }

    /**
     * 更改老师的方法
     */
    public void updateTeacher() {
        //1.调用业务员中的获取方法,获得老师的对象数组
        Teacher[] tecs = teacherService.findAllTeacher();
        //2.判断数组的内存地址,是否为null
        if (tecs == null){
            System.out.println("查无信息,请添加后重试");
            return;
        }
        //3.调用业务员中的deleteStudentById方法,根据ID删除老师
        String updateId = inputTeacherId();
        Teacher newtec =inputTeacherInfo(updateId);
        teacherService.updateTeacher(updateId,newtec);
        System.out.println("修改成功");

    }

    /**
     * 删除老师的方法
     */
    public void deleteTeacherById() {
        //1.调用业务员中的获取方法,获得老师的对象数组
        Teacher[] tecs = teacherService.findAllTeacher();
        //2.判断数组的内存地址,是否为null
        if (tecs == null){
            System.out.println("查无信息,请添加后重试");
            return;
        }
        //3.调用业务员中的deleteStudentById方法,根据ID删除老师

        String delId = inputTeacherId();
        //3.调用业务员中的deleteTeacherById方法,根据ID删除学生
        teacherService.deleteTeacherById(delId);
        //4.提示删除成功
        System.out.println("删除成功");

    }

    /**
     * 抽取出的输入老师ID方法
     * @return
     */
    public String inputTeacherId() {
        String delId;
        while (true) {

            System.out.println("请输入老师ID:");
            delId = sc.next();

            boolean exists = teacherService.isExists(delId);
            if (!exists) {
                System.out.println("您输入的ID不存在,请重新输入:");
            } else {
                break;
            }
        }
        return delId;
    }

    /**
     * 查看老师的方法
     */
    public  void findAllTeacher() {
        //1.调用业务员中的获取方法,获得学生的对象数组
        Teacher[] tecs = teacherService.findAllTeacher();
        //2.判断数组的内存地址,是否为null,不需要遍历
        if (tecs == null){
            System.out.println("查无信息,请添加后重新查询");
            return;
        }

        System.out.println("工号\t\t\t姓名\t\t年龄\t\t生日");

        //3.遍历数组,获取学生的信息并打印纸控制台
        for (int i = 0; i < tecs.length; i++) {
            Teacher tec = tecs[i];
            if (tec != null) {
                System.out.println(tec.getId() + "\t" + tec.getName() + "\t\t" + tec.getAge() + "\t\t" + tec.getBirthday());
            }
        }
    }

    /**
     * 添加老师的方法
     */
    public void addTeacher() {
        String id;
        while (true) {
            System.out.println("请输入教师ID:");
            id = sc.next();
            boolean flag = teacherService.isExists(id);
            if (flag){
                System.out.println("学号已占用,请重新输入");
            }else {
                break;
            }
        }
        Teacher tec = inputTeacherInfo(id);

        //把教师对象传递给TeacherService的addTeacher方法
        TeacherService teacherService = new TeacherService();
        //根据返回值判断是否添加成功
        boolean result = teacherService.addTeacher(tec);
        if(result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }

    /**
     * 抽取出的输入老师信息方法
     * @param id
     * @return
     */
    public Teacher inputTeacherInfo(String id) {
        System.out.println("请输入教师姓名:");
        String name = sc.next();
        System.out.println("请输入教师年龄:");
        String age = sc.next();
        System.out.println("请输入教师生日:");
        String birthday = sc.next();

        Teacher tec = new Teacher();
        tec.setId(id);
        tec.setName(name);
        tec.setAge(age);
        tec.setBirthday(birthday);
        return tec;
    }
}
